package com.kangfu.api.controller;

import com.kangfu.api.interceptor.AuthorizationInterceptor;
import com.kangfu.api.service.IMessageService;
import com.kangfu.common.exception.BusinessException;
import com.kangfu.common.model.AjaxResult;
import com.kangfu.db.model.request.MessageRequest;
import com.kangfu.db.model.vo.MessageInfoVo;
import com.kangfu.db.model.vo.MessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    public IMessageService messageService;

    @GetMapping("/list")
    public AjaxResult list(@RequestAttribute(AuthorizationInterceptor.LOGIN_USER_ID) int userId, @RequestParam(name = "page", defaultValue = "1") int page) {
        List<MessageVo> list = messageService.getMessageList(userId, page);
        return AjaxResult.success(list);
    }

    @GetMapping("/info")
    public AjaxResult info(@RequestAttribute(AuthorizationInterceptor.LOGIN_USER_ID) int userId, @RequestParam(name = "doctorId", defaultValue = "0") int doctorId,  @RequestParam(name = "page", defaultValue = "1") int page, @RequestParam(name = "lastId", defaultValue = "0") int lastId) {
        if (doctorId <= 0) {
            throw new BusinessException("不存在的客服");
        }
        MessageInfoVo messageInfoVo = messageService.getMessageInfo(userId, doctorId, page, lastId);

        return AjaxResult.success(messageInfoVo);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestAttribute(AuthorizationInterceptor.LOGIN_USER_ID) int userId, @RequestBody MessageRequest messageRequest) {
        messageService.addMessage(userId, messageRequest);
        return AjaxResult.success();

    }
}
